<?php

namespace Modules\Goods\Entities;

use Illuminate\Database\Eloquent\Model;

class Goods extends Model
{
    protected $fillable=[ 'name', 'category_id'];
    protected $hidden = ['keywords', 'created_at', 'updated_at'];

    protected $casts = [
        'category_id' => 'integer',
        'min_price' => 'integer',
        'max_price' => 'integer',
        'type' => 'integer',
        'display' => 'boolean',
    ];

    // 引用的外键
    public function category()
    {
        return $this->belongsTo(Category::class);
    }

    public function skus()
    {
        return $this->hasMany(Sku::class);
    }

    public function sku()
    {
        if (empty($this->sku_id))
            return $this->hasOne(Sku::class);
        else
            return $this->belongsTo(Sku::class);
    }
}
